package com.example.byd.ui.home

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.example.byd.R
import com.example.byd.databinding.FragmentHomeBinding
import java.net.InetAddress
import java.net.NetworkInterface
import java.net.SocketException
import java.util.*

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!



    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

//        val textView: TextView = binding.textHome
//        homeViewModel.text.observe(viewLifecycleOwner) {
//            textView.text = it
//        }


       val ipadd: TextView = binding.ipAdd


        val iplist=getLocalIpAddress()











        homeViewModel.ipadd.observe(viewLifecycleOwner) {
            if (iplist != null) {
                var ipstr:String=""
                for(i in iplist){
                    if(i.indexOf(":")>0){
                        continue
                    }

                    ipstr=ipstr+","+i

                    Log.e("bydautosong", i )
                }
                if(ipstr.length>1){
                    ipadd.text="本机ip:"+ipstr.substring(1)
                }

            }else{
                ipadd.text = it
            }

        }

        Log.e("bydautosong", "当前的ip地址为：")
        if (iplist != null) {
            for(i in iplist){
                Log.e("bydautosong", i )
            }
        }




        return root
    }


    fun getLocalIpAddress(): MutableList<String>? {
        try {
            var iplist =  mutableListOf<String>()

            val en: Enumeration<NetworkInterface> = NetworkInterface.getNetworkInterfaces()
            while (en.hasMoreElements()) {
                val intf: NetworkInterface = en.nextElement()
                val enumIpAddr: Enumeration<InetAddress> = intf.getInetAddresses()
                while (enumIpAddr.hasMoreElements()) {
                    val inetAddress: InetAddress = enumIpAddr.nextElement()
                    if (!inetAddress.isLoopbackAddress()) {
                        //return inetAddress.getHostAddress().toString()
                        iplist.add(inetAddress.getHostAddress().toString())
                       // Log.e("bydautosong", inetAddress.getHostAddress().toString())
                    }
                }

            }
            return iplist
        } catch (ex: SocketException) {
            Log.e("bydautosong", ex.toString())
        }
        return null
    }



    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }


}